#include "BspSPI.h"


/*********************************************************************************************
  * @brief  Bsp_SPI1_IOConfig
  * @param  None
  * @retval None
  ********************************************************************************************/
static void Bsp_SPI1_IOConfig(void)
{
	GPIO_InitTypeDef GPIOX;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
	
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_SPI1);
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_SPI1);
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_SPI1);
	
	GPIOX.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
	GPIOX.GPIO_Mode = GPIO_Mode_AF;
	GPIOX.GPIO_OType = GPIO_OType_PP;
	GPIOX.GPIO_PuPd = GPIO_PuPd_UP;
	GPIOX.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_Init(GPIOA,&GPIOX);
}

/*********************************************************************************************
  * @brief  Bsp_SPI2_IOConfig
  * @param  None
  * @retval None
  ********************************************************************************************/
static void Bsp_SPI2_IOConfig(void)
{
	GPIO_InitTypeDef GPIOX;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
	
	GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_SPI2);
	GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF_SPI2);
	GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_SPI2);
	
	GPIOX.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
	GPIOX.GPIO_Mode = GPIO_Mode_AF;
	GPIOX.GPIO_OType = GPIO_OType_PP;
	GPIOX.GPIO_PuPd = GPIO_PuPd_UP;
	GPIOX.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_Init(GPIOB,&GPIOX);
}

/*********************************************************************************************
  * @brief  Bsp_SPI3_IOConfig
  * @param  None
  * @retval None
  ********************************************************************************************/
static void Bsp_SPI3_IOConfig(void)
{
	GPIO_InitTypeDef GPIOX;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
	
	GPIO_PinAFConfig(GPIOC,GPIO_PinSource10,GPIO_AF_SPI3);
	GPIO_PinAFConfig(GPIOC,GPIO_PinSource11,GPIO_AF_SPI3);
	GPIO_PinAFConfig(GPIOC,GPIO_PinSource12,GPIO_AF_SPI3);
	
	GPIOX.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;
	GPIOX.GPIO_Mode = GPIO_Mode_AF;
	GPIOX.GPIO_OType = GPIO_OType_PP;
	GPIOX.GPIO_PuPd = GPIO_PuPd_UP;
	GPIOX.GPIO_Speed = GPIO_Speed_100MHz;
	GPIO_Init(GPIOC,&GPIOX);
}

/*********************************************************************************************
  * @brief  Bsp_SPI1_Config
  * @param  None
  * @retval None
  ********************************************************************************************/
void Bsp_SPI1_Config(void)
{
	SPI_InitTypeDef SPIX;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
	
	Bsp_SPI1_IOConfig();
	
	SPIX.SPI_Mode = SPI_Mode_Master;
	SPIX.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
	SPIX.SPI_DataSize = SPI_DataSize_8b;
	SPIX.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
	SPIX.SPI_FirstBit = SPI_FirstBit_MSB;
	SPIX.SPI_CPHA = SPI_CPHA_2Edge;
	SPIX.SPI_CPOL = SPI_CPOL_High;
	SPIX.SPI_CRCPolynomial = 7;
	SPIX.SPI_NSS = SPI_NSS_Soft;	
	SPI_Init(SPI1,&SPIX);
	
	SPI_Cmd(SPI1,ENABLE);
}

/*********************************************************************************************
  * @brief  Bsp_SPI2_Config
  * @param  None
  * @retval None
  ********************************************************************************************/
void Bsp_SPI2_Config(void)
{
	SPI_InitTypeDef SPIX;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);
	
	Bsp_SPI2_IOConfig();
	
	SPIX.SPI_Mode = SPI_Mode_Master;
	SPIX.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
	SPIX.SPI_DataSize = SPI_DataSize_8b;
	SPIX.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
	SPIX.SPI_FirstBit = SPI_FirstBit_MSB;
	SPIX.SPI_CPHA = SPI_CPHA_2Edge;
	SPIX.SPI_CPOL = SPI_CPOL_High;
	SPIX.SPI_CRCPolynomial = 7;
  SPIX.SPI_NSS = SPI_NSS_Soft;	
	SPI_Init(SPI2,&SPIX);
	
	SPI_Cmd(SPI2,ENABLE);
}

/*********************************************************************************************
  * @brief  Bsp_SPI3_Config
  * @param  None
  * @retval None
  ********************************************************************************************/
void Bsp_SPI3_Config(void)
{
	SPI_InitTypeDef SPIX;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3,ENABLE);
	
	Bsp_SPI3_IOConfig();
	
	SPIX.SPI_Mode = SPI_Mode_Master;
	SPIX.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
	SPIX.SPI_DataSize = SPI_DataSize_8b;
	SPIX.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
	SPIX.SPI_FirstBit = SPI_FirstBit_MSB;
	SPIX.SPI_CPHA = SPI_CPHA_2Edge;
	SPIX.SPI_CPOL = SPI_CPOL_High;
	SPIX.SPI_CRCPolynomial = 7;
  SPIX.SPI_NSS = SPI_NSS_Soft;	
	SPI_Init(SPI3,&SPIX);
	
	SPI_Cmd(SPI3,ENABLE);
}

/*********************************************************************************************
  * @brief  Bsp_SPI1_SendByte
  * @param  uint8_t Byte 需要发送的字节数据
  * @retval uint8_t 读取的字节数据
  ********************************************************************************************/
uint8_t Bsp_SPI1_SendByte(uint8_t Byte)
{
  /*!< Loop while DR register in not empty */
  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);

  /*!< Send Half Word through the sFLASH peripheral */
  SPI_I2S_SendData(SPI1, Byte);

  /*!< Wait to receive a Half Word */
  while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);

  /*!< Return the Half Word read from the SPI bus */
  return SPI_I2S_ReceiveData(SPI1);
}

/*********************************************************************************************
  * @brief  Bsp_SPI2_SendByte
  * @param  uint8_t Byte 需要发送的字节数据
  * @retval uint8_t 读取的字节数据
  ********************************************************************************************/
uint8_t Bsp_SPI2_SendByte(uint8_t Byte)
{
  /*!< Loop while DR register in not empty */
  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);

  /*!< Send Half Word through the sFLASH peripheral */
  SPI_I2S_SendData(SPI2, Byte);

  /*!< Wait to receive a Half Word */
  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);

  /*!< Return the Half Word read from the SPI bus */
  return SPI_I2S_ReceiveData(SPI2);
}

/*********************************************************************************************
  * @brief  Bsp_SPI3_SendByte
  * @param  uint8_t Byte 需要发送的字节数据
  * @retval uint8_t 读取的字节数据
  ********************************************************************************************/
uint8_t Bsp_SPI3_SendByte(uint8_t Byte)
{
  /*!< Loop while DR register in not empty */
  while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET);

  /*!< Send Half Word through the sFLASH peripheral */
  SPI_I2S_SendData(SPI3, Byte);

  /*!< Wait to receive a Half Word */
  while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == RESET);

  /*!< Return the Half Word read from the SPI bus */
  return SPI_I2S_ReceiveData(SPI3);
}

